﻿
using Newtonsoft.Json;

namespace PMIS.Common
{
    public static class ObjectExtension
    {
        public static object? ToObject(this string jsonStr, Type type)
        {
            return JsonConvert.DeserializeObject(jsonStr, type);
        }

        public static T? ToObject<T>(this string jsonStr)
        {
            return JsonConvert.DeserializeObject<T>(jsonStr);
        }

        public static string ToJson(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static T? DeepClone<T>(this T obj) where T : class
        {
            if (obj == null)
            {
                return null;
            }

            return obj.ToJson().ToObject<T>();
        }
    }
}
